

import { ResultState, SDKLogicState, SDKState } from "../../SDKDefine";
import { registerClass } from "../../../cfw/tools/class_decorator";
import BaseAd from "../../core/base/BaseAd";

/**
 * 全局唯一的视频广告实例，通过tt.createRewardedVideoAd来创建。
 */
@registerClass('TTRewardAd')
export default class TTRewardAd extends BaseAd {


    init() {
        this.preload();
    }

    hide(): void {
    }
    load(): void {
        if (this.ad) {
            this.ad.load();
        }
    }
    show() {
        if (this.ad) {
            this.ad.show().then(() => {
            }).catch(() => {
                this.ad.load()
                    .then(() => {
                        this.ad.show()
                    })
                    .catch((err: any) => {
                    })
            })
        }
    }
    onClose(res: any) {
        console.log('NativeTest onClose ', res)
        if (res && res.isEnded || res === undefined) {
            this.callResultCallback(ResultState.YES)
        } else {
            this.callResultCallback(ResultState.NO)
        }
    }


    create(): void {
        // console.log(' 不支持多例')
        if (!this.ad) {
            this.ad = this.sdk.createRewardedVideoAd({
                adUnitId: this.adUnitID,
                // multiton: true,
                // multitonRewardedMsg: "观看下一个可领取更多奖励",
            })
            this.ad.onLoad(this.getFunc(this.onLoad))
            this.ad.onError(this.getFunc(this.onError))
            this.ad.onClose(this.getFunc(this.onClose))
        } else {
            // this.load();
        }
    }

    destroy() {

    }
}
